﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSTMEETING.Win.App
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string ServiceBaseUrl = "http://localhost:6945/";
        public MainWindow()
        {
            InitializeComponent();

            ViewNavigator.Init(this);
            ViewNavigator.Switch(new Views.LoadingView());
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials; //获取或设置发送到主机并用于请求进行身份验证的网络凭据  
            myWebClient.UploadFileAsync(new Uri(ServiceBaseUrl + "api/Signature/Upload"), @"c:\KMPlayer\loading.swf");
            myWebClient.UploadFileCompleted += myWebClient_UploadFileCompleted;
        }

        void myWebClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
        {
            MessageBox.Show("OK");
        }

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(ServiceBaseUrl);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            try
            {
                var product = new DSTMEETING.Web.Api.Contracts.UserLoginParameter()
                {
                    Account = "account",
                    Password = "password"
                };
                var response = await client.PostAsJsonAsync("api/user/login", product);
                var result = await response.Content.ReadAsAsync<DSTMEETING.Data.ApiResult<DSTMEETING.Web.Api.Contracts.UserLoginResult>>();
                response.EnsureSuccessStatusCode(); // Throw on error code（有错误码时抛出）. 
            }
            catch (HttpRequestException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (System.FormatException)
            {
                MessageBox.Show("Price must be a number");
            }
            finally
            {
            }
        }
    }



    /// <summary>
    /// 支持 Session 和 Cookie 的 WebClient。
    /// </summary>
    public class HttpClient2 : WebClient
    {
        // Cookie 容器
        private CookieContainer cookieContainer;
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                HttpWebRequest httpRequest = request as HttpWebRequest;
                httpRequest.CookieContainer = cookieContainer;
            }
            return request;
        }
    }
}
